//
//  LHImageViewCell.m
//  LHImagePicker
//
//  Created by 刘辉 lewis on 2018/6/13.
//  Copyright © 2018年 刘辉 lewis. All rights reserved.
//

#import "LHImageViewCell.h"
#import "NSString+time.h"

@interface LHImageViewCell()
@property(nonatomic,strong)UIImageView *imgView;
@property(nonatomic,strong)UILabel     *durationLabel;
@property(nonatomic,strong)UIButton    *pictureBtn;
@property(nonatomic,strong)UIButton    *selectedBtn;
@end

@implementation LHImageViewCell
#pragma mark - lazy loading
-(UIImageView*)imgView{
    if (_imgView==nil) {
        _imgView=[[UIImageView alloc]initWithFrame:self.contentView.bounds];
    }
    return _imgView;
}
-(UILabel*)durationLabel{
    if (_durationLabel==nil) {
        _durationLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, self.frame.size.height-20, self.frame.size.width-10, 20)];
        _durationLabel.textColor=[UIColor whiteColor];
        _durationLabel.font=[UIFont systemFontOfSize:16];
        _durationLabel.textAlignment=NSTextAlignmentRight;
    }
    return _durationLabel;
}
-(UIButton*)selectedBtn{
    if (_selectedBtn==nil) {
        _selectedBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *imgNormal   = [UIImage imageNamed:@"LHImagePicker.bundle/photo_check_default"];
        UIImage *imgSelected = [UIImage imageNamed:@"LHImagePicker.bundle/photo_check_selected"];
        _selectedBtn.frame = CGRectMake(self.frame.size.width-imgNormal.size.width, 0, imgNormal.size.width, imgNormal.size.height);
        [_selectedBtn setBackgroundImage:imgNormal forState:UIControlStateNormal];
        [_selectedBtn setBackgroundImage:imgSelected forState:UIControlStateSelected];
        _selectedBtn.exclusiveTouch = YES;
        [_selectedBtn addTarget:self action:@selector(pictureDidSelected) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _selectedBtn;
}
-(UIButton*)pictureBtn{
    if (_pictureBtn==nil) {
        _pictureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *imgNormal   = [UIImage imageNamed:@"LHImagePicker.bundle/compose_photograph"];
        UIImage *imgSelected = [UIImage imageNamed:@"LHImagePicker.bundle/compose_photograph_highlighted"];
        UIImage *bgImg       = [UIImage imageNamed:@"LHImagePicker.bundle/compose_photograph_background"];
        [_pictureBtn setBackgroundImage:bgImg forState:UIControlStateNormal];
        _pictureBtn.frame = CGRectMake(0, 0,self.frame.size.width,self.frame.size.height);
        [_pictureBtn setImage:imgNormal forState:UIControlStateNormal];
        [_pictureBtn setImage:imgSelected forState:UIControlStateSelected];
        _pictureBtn.exclusiveTouch = YES;
        [_pictureBtn addTarget:self action:@selector(takePictureBtnDidSelected) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _pictureBtn;
}
#pragma mark -  takePictureBtnDidSelected
-(void)takePictureBtnDidSelected{
    if (_delegate && [_delegate respondsToSelector:@selector(lh_LHImageViewCellPhotoBtnDidSelect:)]) {
        [_delegate lh_LHImageViewCellPhotoBtnDidSelect:self];
    }
}

#pragma mark -  pictureDidSelected
-(void)pictureDidSelected{
    self.selectedBtn.selected=!self.selectedBtn.selected;
    if (self.selectedBtn.selected) {
        if (_delegate && [_delegate respondsToSelector:@selector(lh_LHImageViewCellDidSelectedAsset:)]) {
            [_delegate lh_LHImageViewCellDidSelectedAsset:self];
        }
    }else{
        if (_delegate && [_delegate respondsToSelector:@selector(lh_LHImageViewCellDidDeselectedAsset:)]) {
            [_delegate lh_LHImageViewCellDidDeselectedAsset:self];
        }
    }
    
}
#pragma mark -  init
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self.contentView addSubview:self.imgView];
        [self.contentView addSubview:self.durationLabel];
        [self.contentView addSubview:self.selectedBtn];
        [self.contentView addSubview:self.pictureBtn];
        self.backgroundColor=[UIColor whiteColor];
    }
    return self;
}
#pragma mark - setter
-(void)setAssetModel:(PHAsset *)assetModel{
    if (assetModel==nil) {
        self.pictureBtn.hidden  = NO;
        self.selectedBtn.hidden = YES;
        return;
    }
    self.pictureBtn.hidden  = YES;
    self.selectedBtn.hidden = NO;
    _assetModel=assetModel;
    if (assetModel.mediaType==PHAssetMediaTypeVideo) {
        self.durationLabel.hidden=NO;
        self.durationLabel.text=[NSString durationFromTimerinterval:assetModel.duration];
    }else{
        self.durationLabel.hidden=YES;
    }
    [[PHImageManager defaultManager] requestImageForAsset:assetModel targetSize:CGSizeMake([UIScreen mainScreen].bounds.size.width/3.0-2, [UIScreen mainScreen].bounds.size.width/3.0-2) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (result) {
                self.imgView.image=result;
            }else{
                self.imgView.image=[UIImage imageNamed:@"LHImagePicker.bundle/assets_placeholder_picture"];
            }
        });
    }];
}
-(void)setIsSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    self.selectedBtn.selected = isSelected;
}
@end
